package com.lp.mgt.utils;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;

import sun.misc.BASE64Encoder;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;

public class MD5Util {
	public static Map<String,String> encryption(String username,String password,int hashnum) {
		String salt=null;
		Map<String,String> map=new HashMap<String,String>();
		if(!"".equals(password) && !"".equals(username)) {
			salt=username+"shiro";
			//第一个参数：散列算法 
			SimpleHash simpleHash = new SimpleHash("md5", password, salt, hashnum);
			System.out.println(simpleHash.toString());
			map.put("salt", salt);
			map.put("password", simpleHash.toString());
			return map;
		}else {
			map.put("errMsg", "传入参数有误");
			return map;
		}
	}
	

    public static String EncoderByMd5(String str){
        //确定计算方法
        MessageDigest md5= null;
        String result="";
        try {
            md5 = MessageDigest.getInstance("MD5");
            result=new BASE64Encoder().encode(md5.digest(str.getBytes("utf-8")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        //加密后的字符串
        return result;
    }
	
}
